package calvin.study.springboot.config.ioc;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class LifeCycleBeanPostProcessor implements InstantiationAwareBeanPostProcessor {


	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if (beanName.equals("lifeCycleBean")) {
			System.out.println("1.【Bean实例化阶段：】postProcessBeforeInstantiation被调用(Bean实例化前创建)");
		}
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if (beanName.equals("lifeCycleBean")) {
			System.out.println("3.【Bean实例化阶段：】postProcessAfterInstantiation被调用 (Bean实例化后，属性赋值之前调用)");
		}
		return true;
	}

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
			throws BeansException {
		if (beanName.equals("lifeCycleBean")) {
			System.out.println("4.【Bean实例化后，初始化前调用，赋值阶段：】postProcessProperties被调用");
		}
		return null;
	}
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (beanName.equals("lifeCycleBean")) {
			((LifeCycleBean) bean).setName("中中");
			System.out.println("8. postProcessBeforeInitialization被调用，把name改成中中(Bean初始化之前调用)");
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if (beanName.equals("lifeCycleBean")) {
			((LifeCycleBean) bean).setName("大大");
			System.out.println("11. postProcessAfterInitialization被调用，把name改成大大(Bean初始化之后调用)");
		}
		return bean;
	}

}
